var rewire = require("rewire")
const axios = require('axios')
const fs = require('fs')
const R = require('ramda')
const sharp = require('sharp')
const {parallelMap, errorRetry} = rewire('../common/combinator.cjs')
const {existsFile} = rewire('../common/file.cjs')
const {makeIncArray} = rewire('../common/utils.cjs')
let {splicePics} = rewire('../common/pic.cjs')

const mediaPath = 'e:/media/map'
// const mediaPath = '/tmp/map'

async function joinBigPic(savePath, x1, x2, y1, y2) {
    for (let x = x1; x <= x2; x++) {
	const ypics = R.map(function(y) { return `${mediaPath}/${x}_${y}.png` }, makeIncArray(y1, y2))
	await splicePics(`${mediaPath}/tmp/${x}.png`, 1, ...ypics)
    }
    const xpics = R.map(function(x) { return `${mediaPath}/tmp/${x}.png` }, makeIncArray(x1, x2))
    await splicePics(savePath, 2, ...xpics)
}

joinBigPic('/tmp/big.png', 3210, 3370, 1770, 1990)


// 3260  done
(async function() {
    await splicePics('/tmp/big1.png', 2, ...R.map(function(x) { return `${mediaPath}/tmp/${x}.png` }, makeIncArray(3210, 3260)))
    console.log('跑完0')
    await splicePics('/tmp/big2.png', 2, ...R.map(function(x) { return `${mediaPath}/tmp/${x}.png` }, makeIncArray(3261, 3260 + 50)))
    console.log('跑完1')
    await splicePics('/tmp/big3.png', 2, ...R.map(function(x) { return `${mediaPath}/tmp/${x}.png` }, makeIncArray(3261 + 50, 3260 + 2*50)))
    console.log('跑完2')
    await splicePics('/tmp/big4.png', 2, ...R.map(function(x) { return `${mediaPath}/tmp/${x}.png` }, makeIncArray(3261 + 2*50, 3370)))
    console.log('跑完3')
})()

(async function() {
    await splicePics('/tmp/big_3.png', 2, '/tmp/big3.png', '/tmp/big4.png')
    console.log('跑完')
})()

(async function() {
    await splicePics('/tmp/big__2.png', 2, '/tmp/big2.png', '/tmp/big_3.png')
    console.log('跑完')
})()

(async function() {
    await splicePics('/tmp/big_done.png', 2, '/tmp/big1.png', '/tmp/big__2.png')
    console.log('跑完')
})()



splicePics('e:/media/bigmap2.png', 2, ...R.map(function(x) { return `${mediaPath}/tmp/${x}.png` }, makeIncArray(3210, 3370)))

async function grapMapPies(level, x1, x2, y1, y2) {
    await parallelMap(
	1,
	async function(x) {
	    await parallelMap(
		8,
		async function(y) {
		    const url = `http://gis.chinhphu.vn/BaseMap/${level}/${x}/${y}.png`
		    const filename = `${mediaPath}/${x}_${y}.png`
		    if (!existsFile(filename)) {
			const save = errorRetry(saveFileByUrl)
			await save(url, filename)
		    }
		},
		makeIncArray(y1, y2)
	    )
	},
	makeIncArray(x1, x2)
    )
}


async function saveFileByUrl(url, filename) {
    console.log(url)
    const resp = await axios({
	url,
	responseType: 'arraybuffer',
	timeout: 5000
    })
    fs.writeFileSync(filename, resp.data, 'binary')
}



// (async function() {
//     await grapMapPies(12, 3210, 3370, 1770, 1990)
// })()

//35200

// splicePics('e:/media/aat.png', 1, 'e:/media/map/3210_1904.png', 'e:/media/map/3210_1905.png', 'e:/media/map/3210_1906.png')
splicePics('e:/media/aat.png', 1, 'e:/media/map/3210_1904.png')
joinBigPic('e:/media/bigmap.png', 3210, 3370, 1770, 1990)

(async () => {  try {
    await joinBigPic('e:/media/bigmap1.png', 3210, 3210, 1770, 1870)
    await joinBigPic('e:/media/bigmap2.png', 3210, 3210, 1871, 1990)
    splicePics('e:/media/aat.png', 1, 'e:/media/bigmap1.png', 'e:/media/bigmap2.png')
} catch(e) { console.log(e)} })()

joinBigPic(3210, 3211, 1770, 1990)
joinBigPic('e:/media/bigmap1.png', 3210, 3215, 1770, 1775)

const xpics = R.map(function(x) { return `e:/media/map/tmp/${x}.png` }, makeIncArray(3210, 3215))
splicePics(`e:/media/bigmap.png`, 2, ...xpics)
